package com.xk857.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.config.AuthUtil;
import com.xk857.entities.SysUser;
import com.xk857.entities.Tourism;
import com.xk857.entities.TourismUser;
import com.xk857.feign.IFeignSystemController;
import com.xk857.school.mapper.TourismMapper;
import com.xk857.school.mapper.TourismUserMapper;
import com.xk857.school.req.TourismApplyREQ;
import com.xk857.school.service.ITourismUserService;
import com.xk857.util.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *
 * 用户旅游中间表 服务实现类
 * @author CV大魔王
 * @since 2021-05-03
 */
@Service
@Primary
public class TourismUserServiceImpl extends ServiceImpl<TourismUserMapper, TourismUser> implements ITourismUserService {

    @Autowired
    private IFeignSystemController feignSystemController;

    @Autowired
    private TourismMapper tourismMapper;

    @Override
    @Transactional
    public Result apply(TourismApplyREQ req) {
        // 根据token获取用户信息
        SysUser userInfo = AuthUtil.getUserInfo();
        SysUser user = feignSystemController.findUserByUserId(userInfo.getId());

        // 获取旅游表信息
        Tourism tourism = tourismMapper.selectById(req.getCouriterId());
        // 最大人数
        Integer maxPeople = tourism.getPeopleNum();
        // 查询报名表人数
        QueryWrapper<TourismUser> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "1");
        wrapper.eq("couriter_id", req.getCouriterId());
        // 获取当前状态为报名的当前人数
        Integer countPeople = baseMapper.selectCount(wrapper);
        // 如果报名人数大于或等于当前人数，则报名失败
        if (countPeople >= maxPeople) {
            return Result.error("人数已满");
        }
        // 根据状态、旅游表id,用户id 查询用户是否报名
        wrapper.eq("user_id", user.getId());
        if (baseMapper.selectCount(wrapper) == 1) {
            return Result.error("您已报名");
        }

        // 封装报名数据
        TourismUser tourismUser = new TourismUser();
        tourismUser.setCouriterId(req.getCouriterId());
        tourismUser.setUserId(user.getId());
        tourismUser.setNickName(user.getNickName());
        tourismUser.setSchoolName(req.getSchoolName());
        tourismUser.setPhone(req.getPhone());
        tourismUser.setUserImg(user.getImageUrl());
        tourismUser.setCreateTime(new Date());
        tourismUser.setStatus("1");
        baseMapper.insert(tourismUser);
        return Result.ok("报名成功");
    }


    @Override
    public Result searchByTourismId(String id) {
        QueryWrapper<TourismUser> wrapper = new QueryWrapper<TourismUser>();
        wrapper.eq("couriter_id", id);
        wrapper.eq("status", "1");
        return Result.ok(baseMapper.selectList(wrapper));
    }


    @Override
    public Result cancelRegistration(String couriterId) {
        SysUser userInfo = AuthUtil.getUserInfo();

        QueryWrapper<TourismUser> wrapper = new QueryWrapper<TourismUser>();
        wrapper.eq("couriter_id", couriterId);
        wrapper.eq("user_id", userInfo.getId());
        TourismUser tourismUser = baseMapper.selectOne(wrapper);
        if (tourismUser!=null) {
            baseMapper.deleteById(tourismUser);
            return Result.ok("取消成功");
        }
        return Result.error("您已取消报名，或未报名");
    }

    @Override
    public Result searchCountByTourismId(String id) {
        QueryWrapper<TourismUser> wrapper = new QueryWrapper<TourismUser>();
        wrapper.eq("couriter_id", id);
        wrapper.eq("status", "1");
        return Result.ok(baseMapper.selectCount(wrapper));
    }
}
